package com.demo.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.demo.beans.MusicShare;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MusicService {
    public static void main(String[] args) {
        String name = "再见莫妮卡";
        MusicShare musicShare = musicApi(name);
        System.out.println(musicShare);
    }
    //网易云音乐
    public static   MusicShare  musicApi(String name) {


        MusicShare musicShare  = new MusicShare ();
        String url ="https://api.imjad.cn/cloudmusic/?type=search&search_type=1&s="+name;
        try {
            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpGet get = new HttpGet(url);
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(60000).setConnectionRequestTimeout(60000)
                    .setSocketTimeout(60000).build();  //设置超时
            get.setConfig(requestConfig);

            HttpResponse response = httpclient.execute(get);
            int statusCode = response.getStatusLine().getStatusCode();

            System.out.println(statusCode);

            //if(statusCode==200){
            //获取响应结果
            String result = EntityUtils.toString(response.getEntity());

            System.out.println(result);

            ObjectMapper mapper = new ObjectMapper();

            JsonNode root = mapper.readTree(result);

            Iterator<JsonNode> it = root.get("result").get("songs").iterator();

            List<MusicShare> musicShares = new ArrayList<MusicShare>();

            while (it.hasNext()) {
                MusicShare mu = new MusicShare();

                JsonNode node = it.next();

                String id = node.get("id").asText();
                String title = node.get("name").asText();

                String pictureUrl = node.get("al").get("picUrl").asText();
                String jumpUrl = "https://y.music.163.com/m/song?id="+id+"&app_version=8";

                String summary = "";

                Iterator<JsonNode> summaryIt = node.get("ar").iterator();
                while (summaryIt.hasNext()) {
                    JsonNode de = summaryIt.next();
                    summary = de.get("name").asText();
                }

                mu.setId(id);
                mu.setTitle(title);
                mu.setPictureUrl(pictureUrl);
                mu.setSummary(summary);
                mu.setJumpUrl(jumpUrl);

                musicShares.add(mu);

            }

            int i = musicShares.size();
            int random=(int)(Math.random()*i+0);

            musicShare = musicShares.get(random);

            if(StringUtils.isEmpty(musicShare.getId())) {
                return musicShare;
            }


            url ="https://api.imjad.cn/cloudmusic/?type=song&id="+musicShare.getId();

            get = new HttpGet(url);

            requestConfig = RequestConfig.custom()
                    .setConnectTimeout(60000).setConnectionRequestTimeout(60000)
                    .setSocketTimeout(60000).build();  //设置超时
            get.setConfig(requestConfig);


            response = httpclient.execute(get);
            statusCode = response.getStatusLine().getStatusCode();

            System.out.println(statusCode);

            //if(statusCode==200){
            //获取响应结果
            result = EntityUtils.toString(response.getEntity());

            root = mapper.readTree(result);

            it = root.get("data").iterator();
            String musicUrl = "";

            while (it.hasNext()) {
                JsonNode node = it.next();
                musicUrl = node.get("url").asText();
            }

            musicShare.setMusicUrl(musicUrl);

            System.out.println(musicShare.getId()+""+musicShare.getTitle()+""+musicShare.getSummary()+" "+musicShare.getPictureUrl()+" "+musicShare.getMusicUrl());

        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
            System.out.println(e.toString());

        }
        return musicShare;
    }
}
